package com.collage.ctrl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.collage.entity.Team;
import com.collage.entity.TeamPerson;
import com.collage.params.TeamParams;
import com.collage.service.TeamPersonService;
import com.collage.service.TeamService;
import com.collage.util.Result;
import com.collage.vo.TeamVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.util.*;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/team")
public class TeamController {
    @Resource
    private TeamService service;
    @Resource
    private TeamPersonService tpService;
    /**
     * 团队申请通过标识
     */
    private static final String TEAM_STATUS_SUCCESS = "通过";
    private static final String TEAM_STATUS_RUNNING = "待审核";

    @GetMapping("list")
    public Result getTeamList(TeamParams params) {
        // 当前用户加入的或者创建的团队
        QueryWrapper<TeamPerson> tpQuery = new QueryWrapper<>();
        tpQuery.lambda().eq(TeamPerson::getUid, params.getUid())
                .in(TeamPerson::getStatus, Arrays.asList(TEAM_STATUS_SUCCESS, TEAM_STATUS_RUNNING));
        List<TeamPerson> teamPeople = tpService.list(tpQuery);
        List<String> tidList = teamPeople.stream()
                .map(TeamPerson::getTid).collect(Collectors.toList());
        Map<String, TeamPerson> perMap = new HashMap<>();
        for (TeamPerson teamPerson : teamPeople) {
            perMap.put(teamPerson.getTid(), teamPerson);
        }

        QueryWrapper<Team> tQuery = new QueryWrapper<>();
        if (!StringUtils.isEmpty(params.getName())) {
            tQuery.lambda().like(Team::getName, params.getName());
        }
        if (!StringUtils.isEmpty(params.getUid())) {
            if (!CollectionUtils.isEmpty(tidList)) {
                if (params.getInFlag() > 0) {
                    tQuery.lambda().in(Team::getId, tidList);
                }
            }
        }

        PageHelper.startPage(params.getPageNum(), params.getPageSize());
        PageInfo<Team> pageInfo = new PageInfo<>(service.list(tQuery));
        List<Team> teams = pageInfo.getList();
        if (CollectionUtils.isEmpty(teams)) {
            return Result.success();
        }

        List<TeamVO> voList = new ArrayList<>();
        for (Team team : teams) {
            TeamVO vo = new TeamVO();
            BeanUtils.copyProperties(team, vo);
            if (perMap.containsKey(team.getId())) {
                vo.setStatus(perMap.get(team.getId()).getStatus());
                if (perMap.get(team.getId()).getUid().equals(team.getUid())) {
                    vo.setPersonType("团长");
                } else {
                    vo.setPersonType("团员");
                }
            } else {
                vo.setStatus("未加入");
            }
            voList.add(vo);
        }
        return Result.success(voList, pageInfo.getTotal());
    }

    @PostMapping("edit")
    public Result editTeamInfo(@RequestBody Team team) {
        if (StringUtils.isEmpty(team.getId())) {
            team.setCreateTime(new Date());
            service.save(team);
            TeamPerson tp = new TeamPerson();
            tp.setContext(TEAM_STATUS_SUCCESS);
            tp.setStatus(TEAM_STATUS_SUCCESS);
            tp.setTid(team.getId());
            tp.setUid(team.getUid());
            tpService.save(tp);
        } else {
            service.updateById(team);
        }
        return Result.success();
    }
}